package com.glb.leetcode;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Test {

    public static void main(String[] args) {

      int value = 1 << 2;
      System.out.println(value);
      System.out.println(1 & value);

      // int[] arr = {1,2,3};
      // // 取得数组的各种组合
      // for (int i = 0; i < arr.length; i++) {
      //   for (int j = i+1; j <= arr.length; j++) {
      //     int[] newArr = Arrays.copyOfRange(arr,i, j);
      //     System.out.println(Arrays.toString(newArr));
      //   }
      // }
      
    }

    // main function
    public static void main24(String[] args)
    {
        // Constructor 1
        System.out.println("Using Constructor 1 - ");
 
        // Creating object of class inside main() method
        StringTokenizer st1 = new StringTokenizer(
            "Hello Geeks How are you", " ");
 
        // Condition holds true till there is single token
        // remaining using hasMoreTokens() method
        while (st1.hasMoreTokens())
            // Getting next tokens
            System.out.println(st1.nextToken());
 
        // Constructor 2
        System.out.println("Using Constructor 2 - ");
 
        // Again creating object of class inside main()
        // method
        StringTokenizer st2 = new StringTokenizer(
            "JAVA : Code : String", " :");
 
        // If tokens are present
        while (st2.hasMoreTokens())
 
            // Print all tokens
            System.out.println(st2.nextToken());
 
        // Constructor 3
        System.out.println("Using Constructor 3 - ");
 
        // Again creating object of class inside main()
        // method
        StringTokenizer st3 = new StringTokenizer(
            "JAVA: Code :String", " :", true);
  
        while (st3.hasMoreTokens())
            System.out.println("'" + st3.nextToken() + "'");
    }

  public static void main23(String[] argv) throws Exception {
    // Create a StringBuilder object
    // using StringBuilder() constructor
    StringBuilder str = new StringBuilder();

    str.append("GFG");

    // print string
    System.out.println("String = " + str.toString());

    // create a StringBuilder object
    // using StringBuilder(CharSequence) constructor
    StringBuilder str1 = new StringBuilder("AAAABBBCCCC");

    // print string
    System.out.println("String1 = " + str1.toString());
    System.out.println(str1.capacity());

    // create a StringBuilder object
    // using StringBuilder(capacity) constructor
    StringBuilder str2 = new StringBuilder(10);

    // print string
    System.out.println("String2 capacity = "
        + str2.capacity());
    str2.append("a1234567890");

    System.out.println("String2 capacity = "+ str2.capacity());
    System.out.println("String2 reverse = "+ str2.reverse());
    System.out.println(str2.charAt(1));
    System.out.println(str2.codePointAt(1));
    System.out.println(str2.codePointBefore(1));
    System.out.println(str2.deleteCharAt(1));
    System.out.println(str2.insert(1, "b"));
    System.out.println("String2 capacity = "+ str2.capacity());

    // create a StringBuilder object
    // using StringBuilder(String) constructor
    StringBuilder str3 = new StringBuilder(str1.toString());

    // print string
    System.out.println("String3 = " + str3.toString());
  }

  // Main driver method
  public static void main22(String[] args) {
    StringBuffer s = new StringBuffer("GeeksforGeeks");

    s.delete(0, 5);
    // Returns forGeeks
    System.out.println(s);

    s.deleteCharAt(7);
    // Returns forGeek
    System.out.println(s);
  }

  public static void main21(String args[]) {
    StringBuffer sb = new StringBuffer("Hello ");
    sb.insert(1, "Java");
    sb.append("test");
    // Now original string is changed
    System.out.println(sb);
  }

  public static void main20(String[] args) {
    StringBuffer sb = new StringBuffer();
    sb.append("Hello");
    sb.append(" ");
    sb.append("world");
    String message = sb.toString();
    System.out.println(message);
  }

  public static void main19(String[] args) {
    String test = "abc";
    String test2 = "abc";

    System.out.println(test == test2);
    System.out.println(test.equals(test2));

  }

  volatile static String str = "";
  static int a1 = 0;

  private synchronized static String updateStr(int i) {
    return str + i;
  }

  public static void main18(String[] args) {

    Runnable task = () -> {

      for (int i = 0; i < 5; i++) {
        try {
          Thread.currentThread().sleep(1);
          // str = str + i;
          str = updateStr(i);
          a1++;

        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + ": str=" + str + ", a1=" + a1); // Each thread prints the
                                                                                              // same string
      }
    };

    Thread thread1 = new Thread(task);
    thread1.setName("T-1");

    Thread thread2 = new Thread(task);
    thread2.setName("T-2");

    thread1.start();
    thread2.start();

    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + ": str=" + str + ", a1=" + a1); // Each thread prints the same
                                                                                          // string

    // str = "change";
    // a1=456;
  }

  // Main driver method
  public static void main17(String[] args) {

    // Getting the size of the array
    int[] intArray = new int[5];

    // Adding elements into the array
    // using setInt() method of Array class
    Array.setInt(intArray, 10, 10);

    // Printing the Array content
    // using util.Arrays class
    System.out.println(Arrays.toString(intArray));
  }

  public static void main16(String[] args) {
    // int[][] arr = new int[2][];
    int[][] arr1 = { { 1, 2, 3 }, { 4, 5, 6 } };
    System.out.println(Arrays.deepToString(arr1));
  }

  public static void main15(String[] args) {
    int rows = 4;
    int columns = 4;

    int[][] array = new int[rows][columns];

    int value = 1;
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {
        array[i][j] = value;
        value++;
      }
    }

    System.out.println("The 2D array is: ");
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {
        System.out.print(array[i][j] + "\t");
      }
      System.out.println();
    }
  }

  public static void main14(String args[]) {
    int intArray[][] = { { 1, 2, 3 }, { 4, 5 } };

    int cloneArray[][] = intArray.clone();

    // will print false
    System.out.println(intArray == cloneArray);

    // will print true as shallow copy is created
    // i.e. sub-arrays are shared
    System.out.println(intArray[0] == cloneArray[0]);
    System.out.println(intArray[1] == cloneArray[1]);
  }

  public static void main13(String[] args) {

    // Get the Array
    int intArr[] = { 10, 20, 15, 22, 35 };
    // To sort the array using normal sort
    System.out.println("Integer Array: "
        + Arrays.spliterator(intArr));
  }

  public static void main12(String[] args) {

    // Get the Array
    int intArr[] = { 10, 20, 15, 111, 22, 35, 11, 9 };

    // To sort the array using normal sort
    Arrays.sort(intArr, 1, 3);

    System.out.println("Integer Array: "
        + Arrays.toString(intArr));
  }

  public static void main11(String[] args) {

    // Get the Array
    int intArr[] = { 10, 20, 15, 22, 35 };

    // To sort the array using parallelSort
    Arrays.parallelSort(intArr);

    System.out.println("Integer Array: "
        + Arrays.toString(intArr));
  }

  public static void main10(String[] args) {

    // Get the Arrays
    int intArr[] = { 10, 20, 15, 22, 35 };

    // Get the second Arrays
    int intArr1[] = { 10, 20, 15, 22 };

    // To compare both arrays
    System.out.println("The element mismatched at index: "
        + Arrays.mismatch(intArr, intArr1));
  }

  public static void main9(String[] args) {

    // Get the Array
    int intArr[] = { 10, 20, 15, 22, 35 };

    // To get the hashCode of the arrays
    System.out.println("Integer Array: "
        + Arrays.hashCode(intArr));
  }

  public static void main8(String[] args) {

    // Get the Arrays
    int intArr[] = { 10, 20, 15, 22, 35 };

    int intKey = 22;

    Arrays.fill(intArr, intKey);

    // To fill the arrays
    System.out.println("Integer Array on filling: "
        + Arrays.toString(intArr));
  }

  public static void main7(String[] args) {

    // Get the Arrays
    int intArr[] = { 10, 20, 15, 22, 35 };

    // Get the second Arrays
    int intArr1[] = { 10, 15, 22 };

    // To compare both arrays
    System.out.println("Integer Arrays on comparison: "
        + Arrays.equals(intArr, intArr1));
  }

  public static void main6(String[] args) {

    // Get the Array
    // int intArr[] = { 10, 20, 15, 22, 35 };
    int intArr[][] = { { 10, 20, 15, 22, 35 } };

    // To get the deep String of the arrays
    System.out.println("Integer Array: "
        + Arrays.deepToString(intArr));
  }

  public static void main5(String[] args) {

    // Get the Array
    int intArr[][] = { { 10, 20, 15, 22, 35 } };

    // To get the dep hashCode of the arrays
    System.out.println("Integer Array: "
        + Arrays.deepHashCode(intArr));
  }

  public static void main4(String[] args) {

    // Get the Arrays
    int intArr[][] = { { 10, 20, 15, 22, 35 } };

    // Get the second Arrays
    int intArr1[][] = { { 10, 15, 22 } };

    // To compare both arrays
    System.out.println("Integer Arrays on comparison: "
        + Arrays.deepEquals(intArr, intArr1));
  }

  public static void main3(String[] args) {

    // Get the Array
    int intArr[] = { 10, 20, 15, 22, 35 };

    // To print the elements in one line
    System.out.println("Integer Array: "
        + Arrays.toString(intArr));

    System.out.println("\nNew Arrays by copyOfRange:\n");

    // To copy the array into an array of new length
    System.out.println("Integer Array: "
        + Arrays.toString(
            Arrays.copyOfRange(intArr, 5, 13)));
  }

  // public static void main(String[] args)
  // {

  // int[] copy = new int[10];
  // System.out.println(Arrays.toString(copy));

  // // Get the Array
  // int intArr[] = { 10, 20, 15, 22, 35 };

  // // To print the elements in one line
  // System.out.println("Integer Array: "
  // + Arrays.toString(intArr));

  // System.out.println("\nNew Arrays by copyOf:\n");

  // System.out.println("Integer Array: "
  // + Arrays.toString(
  // Arrays.copyOf(intArr, 10)));
  // System.out.println("Integer Array: " + Arrays.toString(intArr));

  // // // Integer Array: [10, 20, 15, 22, 35]
  // // // New Arrays by copyOf:
  // // // Integer Array: [10, 20, 15]
  // // // Integer Array: [10, 20, 15, 22, 35]
  // }

  public static void main2(String[] args) {
    // Get the Array
    int intArr[] = { 10, 20, 15, 22, 35 };

    Arrays.sort(intArr);

    for (int i : intArr) {
      System.out.print(" " + i);
    }
    System.out.println("");

    int intKey = 115;

    System.out.println(
        intKey
            + " found at index = "
            + Arrays
                .binarySearch(intArr, 1, 3, intKey));
  }
}
